package com.midea.ai.air.common;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.ui.home.MainActivityNew;
import com.midea.ai.air.ui.home.adapter.PopListAdapter;
import com.midea.ai.air.ui.home.data.AdviceGridItem;
import com.midea.ai.air.ui.more.MoreFamilyAddMemberActivity;
import com.midea.ai.air.utils.JumpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by June Kwok on 2015-12-9.
 */
public class ActionBarFragement extends BaseFragment implements View.OnClickListener {

    FrameLayout mBarLayout, mBarFullLayout;
    TextView mTitle;
    private ImageView mIvMenu; //左侧按钮
    private ImageView mIvAdd; //右侧按钮
    private PopupWindow mPopupWindow;

    private int mCurrentPage;

    public final static String SCANNING_GREQUEST_CODE = "ADD_DEVICE";
    public final static String SCANNING_GREQUEST_CODE_ADD = "SCAN_CODE";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setBarFullLayout((FrameLayout) inflater.inflate(R.layout.fragment_action_bar, container, false));
        initView(mBarFullLayout);
        return getBarFullLayout();
    }

    private void initView(View v) {
        mBarLayout = (FrameLayout) v.findViewById(R.id.bar_parent);
        ViewGroup.LayoutParams LP = mBarLayout.getLayoutParams();
        LP.height = getResources().getDimensionPixelSize(R.dimen.height_44dp);
        mBarLayout.setLayoutParams(LP);

        mIvMenu = (ImageView) v.findViewById(R.id.iv_menu);
        mIvAdd = (ImageView) v.findViewById(R.id.iv_add);
        mIvAdd.setOnClickListener(this);
        mIvMenu.setOnClickListener(this);

        mTitle = (TextView) v.findViewById(R.id.tv_device);
    }

    public ImageView getIvMenu() {
        return mIvMenu;
    }

    public ImageView getIvAdd() {
        return mIvAdd;
    }

    public TextView getTitle() {
        return mTitle;
    }

    public void setTitle(TextView mTitle) {
        this.mTitle = mTitle;
    }

    public FrameLayout getBarFullLayout() {
        return mBarFullLayout;
    }

    public void setBarFullLayout(FrameLayout mBarFullLayout) {
        this.mBarFullLayout = mBarFullLayout;
    }

    public void setCurrentPage(int currentPage) {
        mCurrentPage = currentPage;
        createPopupWindow();
    }

    private void createPopupWindow() {
        Resources resources = getResources();
        List<AdviceGridItem> items = new ArrayList<>();
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.layout_popupwindow, null);
        ListView listView = (ListView) contentView.findViewById(R.id.lv_popup_window);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0: //添加家电
                        JumpUtils.toCapture(getActivity(), SCANNING_GREQUEST_CODE_ADD);
                        break;

                    case 1: //添加成员
                        Intent intent = new Intent(getActivity(), MoreFamilyAddMemberActivity.class);
                        intent.putExtra(ConsVal.KEY_TITLE, getString(R.string.air_more_family_add_family_member));
                        intent.putExtra(ConsVal.KEY_ARG, getString(R.string.air_more_family_add));
                        startActivity(intent);
                        break;

                    case 2: //扫一扫
                        JumpUtils.toCapture(getActivity(), SCANNING_GREQUEST_CODE);
                        break;
                }
            }
        });

        switch (mCurrentPage) {
            case ConsVal.ACTIVITY_TAG_HOME:
                items.add(new AdviceGridItem(resources.getString(R.string.home_air_add_device), R.drawable.air_home_add_device));
                items.add(new AdviceGridItem(resources.getString(R.string.home_air_add_member), R.drawable.air_home_add_member));
                items.add(new AdviceGridItem(resources.getString(R.string.home_air_rich_scan), R.drawable.air_home_rich_scan));
                break;
            case ConsVal.ACTIVITY_TAG_DEVICE:
                items.add(new AdviceGridItem(resources.getString(R.string.home_air_add_device), R.drawable.air_device_add_device));
                items.add(new AdviceGridItem(resources.getString(R.string.home_air_add_member), R.drawable.air_device_add_member));
                items.add(new AdviceGridItem(resources.getString(R.string.home_air_rich_scan), R.drawable.air_device_rich_scan));
                listView.setBackgroundResource(R.drawable.air_device_pop_bg);
                break;
        }

        PopListAdapter adapter = new PopListAdapter(getContext(), 0, items);
        adapter.setCurrentPage(mCurrentPage);

        listView.setAdapter(adapter);

        mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        //外部是否可以点击
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        mPopupWindow.setOutsideTouchable(true);
    }

    private void clickAdd(View view) {
        if (mPopupWindow == null) {
            createPopupWindow();
        } else {
            if (mPopupWindow.isShowing()) {
                mPopupWindow.dismiss();
            } else {
                mPopupWindow.showAsDropDown(view, 0, -getResources().getDimensionPixelOffset(R.dimen.height_10dp));
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_add:
                clickAdd(v);
                break;

            case R.id.iv_menu:
                //startActivity(new Intent(getActivity(), HomeSettingFragment.class));
                MainActivityNew activityNew = (MainActivityNew) getActivity();
                activityNew.startOrStopPageSet(true);
                break;
        }

    }

    @Override
    public void onResume() {
        super.onResume();

        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }
}
